#include "stdafx.h"
#include "BloodEmitter.h"

#include "../Kernel.h"

extern Kernel* g_kernel;

#define TIME (g_kernel->GetTime())

const float PI = 3.14159265358979323846f;
#define random()	((rand () & 0x7fff) / ((float)0x7fff))
#define crandom()	(2.0 * (random() - 0.5))


BloodEmitter::BloodEmitter(void)
{
}

BloodEmitter::~BloodEmitter(void)
{
}

/** Update the Emitters */
void BloodEmitter::Update(long dt)
{
    ClearDead();
    float deltaTime = g_kernel->GetFrameTime()/ 1000.0f;
    type_Particles::iterator it = m_particles.begin();
    for(; it!=m_particles.end(); it++)
    {
        Particle &p = *it;

        // kill the particle
        if ( TIME > p.lifeTime )
        {
            p.isDead = true;
            continue;
        }
		
		bool fadeFast = false;

		if(it->lifeTime - TIME > 900)
		{

			//convert degrees to radians
			double radians = p.angle / 360.0f * 2.0f * PI;
			p.position = p.position + Vector2f(cos(radians) * deltaTime * p.velocity.x *8 ,sin(radians)*deltaTime*p.velocity.y * 8);

		}
		else
			fadeFast = true;


		// decreate the alpha value
        if ( TIME > p.nextAlphaUpdate )
        {
			if(fadeFast)
				p.alpha -= 0.01f; 
			p.nextAlphaUpdate = TIME + p.fadeTime;
        }

        Vector3f color = EndColor - p.color;
        Vector3f fade  = color * ColorFadeTime;
        p.color = p.color + fade;
        
    }

    if ( (m_particles.size() < MaxParticles) && TIME > m_spawnTimer && MaxTotalParticles >= m_particlesCreated )
    {
        Spawn();
        m_spawnTimer = TIME + SpawnRate;

    }

}

/** Render the Emitters */
void BloodEmitter::Render(IRender* r, ICamera* cam )
{
    type_Particles::iterator it = m_particles.begin();
    for(; it!=m_particles.end(); it++)
	{

        r->SetColor( it->color.GetX(), it->color.GetY(), it->color.GetZ(), it->alpha  );
        Vector2f position( it->position - cam->GetPosition() );
        
		if(it->lifeTime - TIME < 900)
		{
			double radians = it->angle / 360.0f * 2.0f * PI;
			r->FillCircle(2.5,position.x + 7*cos(radians),position.y + 7*sin(radians));
		}
		else
			r->FillRectRotate(position.x, position.y, it->size, it->size / 3.5, it->angle);
    }
}

/** Spawn a blood particle */
void BloodEmitter::Spawn()
{
    Particle p;
    p.color    = StartColor;

    CalculateJitter( p, InitVel );
    p.fadeTime = AlphaFadeTime;
    p.position = Position;
    // p.velocity = InitVel;
    p.lifeTime = TIME + LifeSpan;

	p.velocity.x = abs(crandom() * 15) + 4;
	p.velocity.y = abs(crandom() * 15) + 4;

	//angle in degrees
	Vector2f direction = EntityManager::GetInstance()->GetPlayer()->m_vForward;
	if(direction.x == 1)
		p.angle = crandom() * 35;
	else if(direction.x == -1)
		p.angle = crandom() * 35 + 180;
	else if(direction.y == 1)
		p.angle = crandom() * 35 + 90;
	else
		p.angle = crandom() * 35 + 270;
	

    p.acceleration = Acceleration;
    p.alpha = StartAlpha;
    p.size = ParticleSize;

    m_particlesCreated++;
    m_particles.push_back( p );

}